var adminCache = {
    SideMenuRawData: {} //返回的原始数据
};
var SideBarULStyles = ['nav-second-level', 'nav-third-level','fake-2','fake-3','fake-4','fake-5','fake-6','fake-7'];


//$(function () {
//    $('#side-menu').metisMenu();
//});

//Loads the correct sidebar on window load,
//collapses the sidebar on window resize.
// Sets the min-height of #page-wrapper to window size
$(function () {
    loadNavigatorbarMenu();
    $(window).bind("load resize", function () {
        var topOffset = 50;
        var width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        if (width < 768) {
            $('div.navbar-collapse').addClass('collapse');
            topOffset = 100; // 2-row-menu
        } else {
            $('div.navbar-collapse').removeClass('collapse');
        }

        var height = ((this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height) - 1;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height) + "px");
        }
    });
});
/**
 * after page load set current active page link
 * @private
 */
function setActiveLinkStyle() {
    //after load side nav
    var url = window.location;
    var element = $('ul.nav a').filter(function () {
        if($(this).attr('href')=='#') {
            return false;
        }
        return this.href == url || url.href.indexOf(this.href) == 0;
    }).addClass('active').parent().parent().addClass('in').parent();
    if (element.is('li')) {
        element.addClass('active');
    }
}

/**
 * 加载管理主面导航区域菜单数据
 * 页面加载时就加载，不等页面ready
 */
function loadNavigatorbarMenu() {
    var url = ctx + '/admin/menu/getAdminMenu.do';
    var pms = {
        deeps: 5
    };
    $.getJSON(url, pms, function (resp) {
        if (resp.status) {
            adminCache.SideMenuRawData = resp.data;
            buildMenubarHtml(resp.data);
        } else {
            log('管理页面导航菜单加载失败' + resp.data);
        }
    });
}

/**
 * 构建管理页面上的上方导航区域HTMML
 * 添加a链接的单击事件处理
 *
 * @param {}
 *            data
 */
function buildMenubarHtml(data) {
    var rootElement = $('#pageLeftSideBar ul#side-menu');
    if (data.subTree && data.subTree.length > 0) {
        rootElement.empty();

        var str = '';
        $.each(data.subTree, function () {
            str = str.concat(iterateGenerateMenubarHtml(this, 0));
        });
        //rootElement.html(str);
        rootElement.append(str);
    }
    $('#side-menu').metisMenu();
    setActiveLinkStyle();
}
/**
 * 遍历生成导航菜单HTML
 *
 * @param {Object}
 *            node 要遍历的节点
 * @param {Number} ulStyleIndex li元素内ul样式序号
 * @return {} 此节点生成的HTML节点
 */
function iterateGenerateMenubarHtml(node, ulStyleIndex) {
    var str = '<li>';
    str = str.concat('<a href="').concat(utils.isBlank(node.linkAddress) ? '#' : utils.adjustUrl(node.linkAddress)).concat('" ');
    str = str.concat(utils.isBlank(node.remark) ? '' : (' title="' + node.remark + '" '));
    str = str.concat('funCode="').concat(node.id).concat('" ');
    if (!utils.isBlank(node.linkTarget) && node.linkTarget != '_self') {
        str = str.concat(' target="').concat(utils.trimNull(node.linkTarget)).concat('" ');
    }
    str = str.concat('>');
    if (!utils.isBlank(node.linkIconClass)) {
        str = str.concat('<i class="fa ' + node.linkIconClass + ' fa-fw"></i>');
    }
    str = str.concat(node.linkName);
    var childFlag = false;
    if (node.subTree && node.subTree.length > 0) {
        childFlag = true;
        str = str.concat('<span class="fa arrow"></span>');
    }
    str = str.concat('</a>');//end of <a>

    if (childFlag) {
        if (ulStyleIndex < SideBarULStyles.length) {
            str = str.concat('<ul class="nav ' + SideBarULStyles[ulStyleIndex] + ' collapse">');
        } else {
            str = str.concat('<ul class="nav">');
        }
        $.each(node.subTree, function () {
            str = str.concat(iterateGenerateMenubarHtml(this, ulStyleIndex + 1));
        });
        str = str.concat('</ul>');
    }
    str = str.concat('</li>');
    return str;
}